import axios, { type AxiosRequestHeaders, type AxiosResponse } from 'axios'
import { ElMessage } from 'element-plus'

import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

interface ResponseType<T> {
  code: number
  data: T
  message: string
  success: boolean
}
const request = axios.create({
  baseURL: '/kl',
  timeout: 5000
})

// 请求拦截器
request.interceptors.request.use((config) => {
  // let token = localStorage.getItem("token1");
  // if (token) {
  //   (config.headers as AxiosRequestHeaders).token = token;
  // }
  ;(config.headers as AxiosRequestHeaders).Authorization = 'LTF_token'
  Nprogress.start()
  return config
})
// 响应拦截器
request.interceptors.response.use(
  (response: AxiosResponse<ResponseType<any>>) => {
    const res = response.data
    Nprogress.done()
    if (res.code !== 200) {
      return Promise.reject(response.data.message || '')
    } else {
      return res.data
    }
  },
  (error) => {
    // do something with request error
    Nprogress.done()
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

export default request
